class Solution
{
public:
    string reverseWords(string s)
    {
        int wordBegin = 0;
        int wordEnd = 0;
        int n = s.size();
        while (true)
        {
            if (wordEnd == n || s[wordEnd] == ' ')
            {
                int left = wordBegin;
                int right = wordEnd - 1;
                while (left < right)
                {
                    swap(s[left], s[right]);
                    ++left;
                    --right;
                }
                wordBegin = wordEnd + 1;
            }
            if (wordEnd == n)
            {
                break;
            }
            else
            {
                ++wordEnd;
            }
        }
        return s;
    }
};